package cn.autumnorange.app.common.provider.cacheannotation.aop;

import cn.autumnorange.app.common.provider.cacheannotation.RedisCacheTarget;
import cn.autumnorange.app.common.provider.cacheannotation.strategy.RedisCacheReadAnnotationnStrategy;
import cn.autumnorange.app.common.rpc.anntation.AnnotationContext;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.redisson.api.RedissonClient;

import java.lang.annotation.Annotation;
import java.lang.reflect.Parameter;

/**
 * 缓存读取加读写锁切面
 */
public class RedisMethodInterceptor implements MethodInterceptor {
  /**
   * 全局注解容器 设置当前缓存读取加锁切面
   */
  private AnnotationContext annotationContext = AnnotationContext.getAnnotationContext();

  public RedisMethodInterceptor(RedissonClient redissonClient, long connectTimeout, long readTimeout) {

     RedisCacheReadAnnotationnStrategy redisCacheReadAnnotationnStrategy =
              new RedisCacheReadAnnotationnStrategy(redissonClient, connectTimeout, readTimeout);
      annotationContext.putAnnotationStrategy(
              RedisCacheReadAnnotationnStrategy.ANNOTATIONSTRATEGY, redisCacheReadAnnotationnStrategy);
  }

  @Override
  public Object invoke(MethodInvocation methodInvocation) throws Throwable {


//      annotationContext.putAnnotationStrategy(
//              RedisCacheTarget.class.getName(), redisCacheReadAnnotationnStrategy);
    //       return methodInvocation.proceed();

    ////        testAOP=new TestAOP(redissonClient);
    ////        testAOP.setAnnotation(annotation);
    //////
    // testAOP.setRedissonClient(applicationContext.getBean(RedissonClient.class));
    ////        return testAOP.annotationTargetHandle(methodInvocation);

    // 获取注解参数
//    Parameter[] parameters = methodInvocation.getMethod().getParameters();
//    for (Parameter parameter : parameters) {
//      Class<?> classtype = parameter.getType();
//      if (classtype != null) {
//        System.out.println("classtype: " + classtype.getName());
//      }
//      System.out.println("parameter " + parameter.getName());
//    }

    return annotationContext.annotationTargetHandle(RedisCacheReadAnnotationnStrategy.ANNOTATIONSTRATEGY,methodInvocation);

    //        String cacheNameAndType = redisCacheTargt.cacheNameAndType();
    //        String[] cacheNameAndTypeArray = cacheNameAndType.split("#");
    //        // 获取方法参数名
    //        ParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer();
    //        String dataType = cacheNameAndTypeArray[0];
    //        // 获取redis缓存数据
    //        // 读缓存加读锁 ,获取不到读数据库加写锁将数据库数据写缓存
    //        //     写数据加写锁 写数据库,写缓存
    ////        RReadWriteLock rReadWriteLock =
    ////                redissonClient.getReadWriteLock("testlock" + cacheNameAndTypeArray[1]);
    ////
    ////        RLock readRLock = rReadWriteLock.readLock();
    ////        //    // 对指定参数加读锁,有写锁时会被卡住
    ////        readRLock.lock(5000, TimeUnit.MILLISECONDS);
    //        return  methodInvocation.proceed();
  }
  //
  //    public RedissonClient getRedissonClient() {
  //        return redissonClient;
  //    }
  //
  //    public void setRedissonClient(RedissonClient redissonClient) {
  //        this.redissonClient = redissonClient;
  //    }
}
